package org.hibernate.search;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

/**
 *
 * @author liubin
 *
 * @date 2018年2月28日下午5:21:56
 */
public class AnalyzerTest {
    private static final String[] examples =
            {"The quick brown fox jumped over the lazy dogs", "美国民主党总统候选人希拉利是前总统克林顿的夫人"};

    private static final Analyzer[] analyzers = new Analyzer[] {new WhitespaceAnalyzer(),
            new SimpleAnalyzer(), new StopAnalyzer(), new StandardAnalyzer(), //new MIK_CAnalyzer(), // 需要引入IKAnalyzer.jar
            new ChineseAnalyzer(), new CJKAnalyzer(), //new ThesaurusAnalyzer() // 需要引入Thesaurus.jar
    };

    public static void main(String[] args) throws IOException {
        String[] strings = examples;
        if (args.length > 0) {
            strings = args;
        }
        for (int i = 0; i < strings.length; i++) {
            analyze(strings[i]);
        }
    }

    private static void analyze(String text) throws IOException {
        System.out.println("Analyzing \"" + text + "\"");
        for (int i = 0; i < analyzers.length; i++) {
            Analyzer analyzer = analyzers[i];
            String name = analyzer.getClass().getName();
            name = name.substring(name.lastIndexOf(".") + 1);
            System.out.println(" " + name + ":");
            System.out.print(" ");
            displayTokens(analyzer, text);
            System.out.println("\n");
        }
    }

    public static void displayTokens(Analyzer analyzer, String text) throws IOException {
        Token[] tokens = tokensFromAnalysis(analyzer, text);
        for (int i = 0; i < tokens.length; i++) {
            Token token = tokens[i];
//            System.out.print("[" + token.termText() + "] ");
        }
    }

    public static Token[] tokensFromAnalysis(Analyzer analyzer, String text) throws IOException {
        TokenStream stream = analyzer.tokenStream("contents", new StringReader(text));
        ArrayList tokenList = new ArrayList();

        while (true) {
//            Token token = stream.next();
//            if (token == null) break;
//            tokenList.add(token);
        }
    }

}
